<template>
	<view class="template-build tn-safe-area-inset-bottom">
		<!-- 顶部自定义导航 -->
		<tn-nav-bar fixed alpha customBack>
			<view slot="back" class='tn-custom-nav-bar__back' @click="goBack">
				<text class='icon tn-icon-left'></text>
				<text class='icon tn-icon-home-capsule-fill'></text>
			</view>
		</tn-nav-bar>

		<view class="tn-safe-area-inset-bottom" :style="{paddingTop: vuex_custom_bar_height + 'px'}">

			<view class="tn-margin tn-bg-gray--light" style="border-radius: 10rpx;padding: 20rpx 30rpx;">
				<input placeholder="请填写圈子名称 (12字内)" maxlength="12" name="input" placeholder-style="color:#AAAAAA"
				v-model="dataObj.qunbt" :disabled="id"></input>
			</view>
			<view class="tn-margin tn-bg-gray--light tn-padding" style="border-radius: 10rpx;">
				<textarea maxlength="500" placeholder="请输入圈子介绍 (500字内)" placeholder-style="color:#AAAAAA"
				v-model="dataObj.qunjj"></textarea>
			</view>

			<view class="tn-flex tn-flex-row-between tn-flex-col-center tn-padding-top-xl tn-margin">
				<view class="tn-flex justify-content-item">
					<view class="tn-bg-black tn-color-white tn-text-center"
						style="border-radius: 100rpx;margin-right: 8rpx;width: 45rpx;height: 45rpx;line-height: 45rpx;">
						<text class="tn-icon-image" style="font-size: 30rpx;"></text>
					</view>
					<view class="">
						<text class="tn-text-lg tn-padding-right-xs tn-text-bold">圈子图片</text>
						<text class="tn-text-xs tn-padding-right-xs tn-color-grey">首张为圈子头像，长按排序</text>
					</view>
				</view>
				<view class="justify-content-item tn-text-df tn-color-grey" @tap="clear">
					<text class="tn-padding-xs">清空上传</text>
					<text class="tn-icon-delete"></text>
				</view>
			</view>

			<view class="tn-margin-left tn-padding-top-xs">
				<tn-image-upload-drag ref="imageUpload" :action="action" :width="236" :height="236"
					:fileList="dataObj.fjList" :disabled="disabled" :autoUpload="autoUpload" :maxCount="maxCount"
					:showUploadList="showUploadList" :showProgress="showProgress" :deleteable="deleteable"
					:customBtn="customBtn" @sort-list="onSortList" @onChooseComplete="onChoose" @onRemove="onRemove" />

			</view>

			<view class="tn-margin tn-bg-gray--light" style="border-radius: 10rpx;padding: 20rpx 30rpx;">
				<input placeholder="请填写入圈费用 (只允许输入数字且大于等于1)" maxlength="12" placeholder-style="color:#AAAAAA"
				  v-model="dataObj.jrje" type="number"></input>
			</view>
			<view class="" style="border-radius: 10rpx;padding: 0rpx 30rpx;">
				<text style="font-size: 26rpx;color: #f56c6c;">入圈费用每年可调整5次</text>
			</view>
			<view class="tn-flex tn-flex-row-between tn-flex-col-center tn-padding-top-xl tn-margin">
				<view class="tn-flex justify-content-item">
					<view class="tn-bg-black tn-color-white tn-text-center"
						style="border-radius: 100rpx;margin-right: 8rpx;width: 45rpx;height: 45rpx;line-height: 45rpx;">
						<text class="tn-icon-location" style="font-size: 30rpx;"></text>
					</view>
					<view class="tn-text-lg tn-padding-right-xs tn-text-bold">圈子地点</view>
				</view>
				<view class="justify-content-item tn-text-df tn-color-grey">
					<view class="tn-padding-xs" v-if="dataObj.qundz">
						<input type="text" v-model="dataObj.qundz">
					</view>
					<text class="tn-padding-xs" @click="getAddress" v-else>选择<text class="tn-icon-right"></text></text>
					<!-- <text class="tn-icon-right"></text> -->
				</view>
			</view>

			<!-- <view class="tn-flex tn-flex-row-between tn-flex-col-center tn-padding-top-xl tn-margin">
				<view class="tn-flex justify-content-item">
					<view class="tn-bg-black tn-color-white tn-text-center"
						style="border-radius: 100rpx;margin-right: 8rpx;width: 45rpx;height: 45rpx;line-height: 45rpx;">
						<text class="tn-icon-qr-code" style="font-size: 30rpx;"></text>
					</view>
					<view class="tn-text-lg tn-padding-right-xs tn-text-bold">微信圈子群</view>
				</view>
				<view class="justify-content-item tn-text-df tn-color-grey">
					<text class="tn-padding-xs">上传</text>
					<text class="tn-icon-right"></text>
				</view>
			</view> -->

			<view class="tn-flex tn-flex-row-between tn-flex-col-center tn-padding-top-xl tn-margin">
				<view class="tn-flex justify-content-item">
					<view class="tn-bg-black tn-color-white tn-text-center"
						style="border-radius: 100rpx;margin-right: 8rpx;width: 45rpx;height: 45rpx;line-height: 45rpx;">
						<text class="tn-icon-assort" style="font-size: 30rpx;"></text>
					</view>
					<view class="tn-text-lg tn-padding-right-xs tn-text-bold">圈子分类</view>
				</view>
				<view class="justify-content-item tn-text-df tn-color-grey">
					<text class="tn-padding-xs">选择</text>
					<text class="tn-icon-right"></text>
				</view>
			</view>

			<view class="tn-tag-content tn-margin tn-text-justify tn-padding-bottom">
				<view v-for="(item, index) in tagList" :key="index" @click="checkLabel(index)"
					class="tn-tag-content__item tn-margin-right tn-round tn-text-sm tn-text-bold"
					:class="[`tn-bg-${item.bqcolor}--light tn-color-${item.bqcolor} ${getActiveStyle(item.isCheck)}`]">
					<text class="tn-tag-content__item--prefix">#</text> {{ item.bqname }}
				</view>
			</view>

			<!-- 悬浮按钮-->
			<view class="tn-flex tn-footerfixed">
				<view class="tn-flex-1 justify-content-item tn-margin-sm tn-text-center">
					<tn-button :backgroundColor="isRequest?'#3f3f3f':'#00FFC6'" padding="40rpx 0" width="60%" shadow fontBold @click="upload">
						<!-- <text class="tn-icon-light tn-padding-right-xs tn-color-black"></text> -->
						<text class="tn-color-black">{{id?'修 改':'创 建'}}</text>
						<text class="tn-icon-topics tn-padding-left-xs tn-color-black"></text>
					</tn-button>
				</view>
			</view>

		</view>

		<view class='tn-tabbar-height'></view>

	</view>
</template>

<script>
	import Tools from '@/tools/config.js'
	import template_page_mixin from '@/libs/mixin/template_page_mixin.js'
	export default {
		name: 'TemplateBuild',
		mixins: [template_page_mixin],
		data() {
			return {
				tagList: [
					// {
					// 	color: 'red',
					// 	title: "学校",
					// },
					// {
					// 	color: 'cyan',
					// 	title: "情感",
					// },
					// {
					// 	color: 'blue',
					// 	title: "电影",
					// },
					// {
					// 	color: 'green',
					// 	title: "美食",
					// },
					// {
					// 	color: 'orange',
					// 	title: "景点",
					// },
					// {
					// 	color: 'purplered',
					// 	title: "旅游",
					// },
					// {
					// 	color: 'purple',
					// 	title: "宠物",
					// },
					// {
					// 	color: 'brown',
					// 	title: "设计",
					// },
					// {
					// 	color: 'yellowgreen',
					// 	title: "摄影",
					// },
					// {
					// 	color: 'grey',
					// 	title: "运动",
					// },
					// {
					// 	color: 'orangered',
					// 	title: "其他",
					// }
				],
				action: Tools.host + '/tpup.aspx', //'https://www.hualigs.cn/api/upload',
				// action: '',
				formData: {
					apiType: 'this,ali',
					token: 'dffc1e06e636cff0fdf7d877b6ae6a2e',
					image: null
				},
				fileList: [
				// 	{
				// 	url: 'https://resource.tuniaokj.com/images/blogger/onepiece-1.jpg'
				// }, {
				// 	url: 'https://resource.tuniaokj.com/images/blogger/onepiece-2.jpg'
				// }, {
				// 	url: 'https://resource.tuniaokj.com/images/blogger/onepiece-3.jpg'
				// }, {
				// 	url: 'https://resource.tuniaokj.com/images/blogger/content_1.jpeg'
				// },
				],
				showUploadList: true,
				customBtn: false,
				autoUpload: false,
				showProgress: false,
				deleteable: true,
				customStyle: false,
				maxCount: 9,
				disabled: false,
				dataObj:{
					qundz:"",
					qunbt:"",
					qunrs:"",
					qunwxqtp:"",
					iid:"",
					bqlist:[],
					tplist:[],
					bqname:"",
					qunjj:"",
					fjList:[],
					quntp:"",
					jrje:"",
					bqxx:[{
						bqcolor:"",
						bqname:"",
					}],
				},
				id:null,
				isRequest: false,
			}
		},
		onLoad(options) {
			// console.log(options)
			this.id = options.id
			this.initData(options.id)
		},
		methods: {
			initData(id){
				let theopenid = uni.getStorageSync('openid')
				let theunionid = uni.getStorageSync('unionid')
				uni.request({
					url: Tools.host + '/wshly.ashx',
					method: 'POST',
					data: {
						'lx': 'qun_xzxg_s',
						'openid': theopenid,
						'unionid': theunionid,
						'quniid': id ?? "",
						'qun_xx':"",
						'bqxx':"",
					},
					success: (res) => {
						//成功
						if (res.data.return_code == '1') {
							var thejson = JSON.parse(res.data.data)
							console.log(thejson)
							this.tagList = thejson.bqlist.map(i=>{
								return {
									...i,
									isCheck: false
								}
							})
							if(thejson.qunxx){
								thejson.qunxx.fjList = thejson.qunxx.tplist
								this.dataObj = thejson.qunxx
								this.tagList = this.tagList.map(i=>{
									const a = thejson.qunxx.bqname.split(",")
									const b = a.findIndex(j=>j == i.bqname)
									if(b != -1){
										i.isCheck = true
									}
									return i
								})
								this.dataObj.iid = this.id
								this.$nextTick(()=>{
									this.$refs['imageUpload'].setData(this.dataObj.fjList)
								})
								console.log(this.dataObj)
							}
						}
					},
					fail: (res) => {
						console.log('失败' + res)
						uni.showModal({
							title: '失败，请稍后重试',
							showCancel: false
						})
					}
				});
			},
			checkLabel(index){
				this.tagList[index].isCheck = !this.tagList[index].isCheck
			},
			getActiveStyle(type){
				let str = ""
				if(type){
					str = "active"
				}
				return str
			},
			getAddress(){
				// uni.getLocation({
				// 	geocode:true,
				// 	success: (e) => {
				// 		console.log(e)
				// 	}
				// })
				uni.chooseLocation({
					success: (e) => {
						console.log(e)
						this.dataObj.qundz = e.address
					},
					fail(err) {
						console.log(err)
					}
				})
			},
			onRemove(e){
				console.log(e)
				this.dataObj.fjList.splice(e,1)
			},
			onChoose(e) {
				console.log("111111", e)
				let theopenid = uni.getStorageSync('openid')
				let theunionid = uni.getStorageSync('unionid')
				uni.uploadFile({
					url: this.action, //仅为示例，非真实的接口地址
					filePath: e[e.length - 1].url,
					name: 'file',
					formData: {
						'lx': 'tpup',
						'openid': theopenid,
						'unionid': theunionid,
					},
					success: (uploadFileRes) => {
						// console.log(uploadFileRes.data);
						const a = JSON.parse(uploadFileRes.data)
						console.log(a.data)
						this.dataObj.fjList.push({
							url:a.data
						})
						console.log(this.dataObj.fjList)
					}
				});
			},
			// 跳转
			tn(e) {
				uni.navigateTo({
					url: e,
				});
			},
			// 手动上传文件
			upload() {
				// this.$refs.imageUpload.upload()
				if(this.dataObj.jrje == 0){
					uni.showToast({
						title:"入圈金额不能为0",
						icon:"none"
					})
					return
				}
				if(this.isRequest) return
				this.isRequest = true
				uni.showLoading()
				this.dataObj.quntp = this.dataObj.fjList.map(i=>i.url).join()
				this.dataObj.bqname = this.tagList.filter(i=>{
					if(i.isCheck) {
						return i
					}
				}).map(i=>i.bqname).join()
				// console.log(this.dataObj)
				let theopenid = uni.getStorageSync('openid')
				let theunionid = uni.getStorageSync('unionid')
				uni.request({
					url: Tools.host + '/wshly.ashx',
					method: 'POST',
					data: {
						'lx': 'qun_xzxg',
						'openid': theopenid,
						'unionid': theunionid,
						'qunxx': this.dataObj
						// 'quntp':this.dataObj.quntp,
						// 'bqname': this.dataObj.bqname,
						// 'qundz': this.dataObj.qundz,
						// 'qunjj': this.dataObj.qunjj,
						// 'qunbt': this.dataObj.qunbt,
						// 'iid': this.id ?? ""
					},
					success: (res) => {
						uni.hideLoading()
						//成功
						if (res.data.return_code == '1') {
							uni.showToast({
								title:this.id?"圈子修改成功":"圈子创建成功",
								icon:"success",
								duration: 2000
							})
							setTimeout(()=>{
								uni.redirectTo({
									url:"/minePages/circle"
								})
								// uni.navigateBack()
							},2000)
						}else{
							setTimeout(()=>{
								uni.showToast({
									title:res.data.return_msg,
									icon:"none"
								})
							},100)
						}
					},
					fail: (res) => {
						uni.hideLoading()
						console.log('失败' + res)
						uni.showModal({
							title: '失败，请稍后重试',
							showCancel: false
						})
					},
					complete:()=> {
						this.isRequest = false
						// uni.hideLoading()
					}
				});
			},
			// 手动清空列表
			clear() {
				this.dataObj.fjList = []
				this.$refs.imageUpload.clear()
			},
			// 图片拖拽重新排序
			onSortList(list) {
				console.log(list);
			},
		}
	}
</script>

<style lang="scss" scoped>
	.active{
		border: 2rpx solid #000000;
	}
	.template-edit {}

	/* 胶囊*/
	.tn-custom-nav-bar__back {
		width: 100%;
		height: 100%;
		position: relative;
		display: flex;
		justify-content: space-evenly;
		align-items: center;
		box-sizing: border-box;
		background-color: rgba(0, 0, 0, 0.15);
		border-radius: 1000rpx;
		border: 1rpx solid rgba(255, 255, 255, 0.5);
		color: #FFFFFF;
		font-size: 18px;

		.icon {
			display: block;
			flex: 1;
			margin: auto;
			text-align: center;
		}

		&:before {
			content: " ";
			width: 1rpx;
			height: 110%;
			position: absolute;
			top: 22.5%;
			left: 0;
			right: 0;
			margin: auto;
			transform: scale(0.5);
			transform-origin: 0 0;
			pointer-events: none;
			box-sizing: border-box;
			opacity: 0.7;
			background-color: #FFFFFF;
		}
	}

	/* 底部悬浮按钮 start*/
	.tn-tabbar-height {
		min-height: 100rpx;
		height: calc(120rpx + env(safe-area-inset-bottom) / 2);
	}

	.tn-footerfixed {
		position: fixed;
		width: 100%;
		bottom: calc(30rpx + env(safe-area-inset-bottom));
		z-index: 1024;
		box-shadow: 0 1rpx 6rpx rgba(0, 0, 0, 0);

	}

	/* 底部悬浮按钮 end*/

	/* 标签内容 start*/
	.tn-tag-content {
		&__item {
			display: inline-block;
			line-height: 45rpx;
			padding: 10rpx 30rpx;
			margin: 20rpx 20rpx 5rpx 0rpx;

			&--prefix {
				padding-right: 10rpx;
			}
		}
	}

	/* 标签内容 end*/
</style>